package jerry;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *  This class creates a quitter: you must close the server using this class.
 * @author Alby87
 */
public class QuitterTCP extends Thread {
    UsersManager userManager;
    
    public QuitterTCP (UsersManager userManager) {
        this.userManager=userManager;
    }

    @Override
    public void run() {
        System.out.println("Type 'quit' and press Enter to quit");
        while(true) {
	        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	        try {
	            String st = br.readLine();
	            if("quit".equals(st)) {
	            	break;
	            }
	        } catch (IOException ex) {
	            Logger.getLogger(QuitterTCP.class.getName()).log(Level.SEVERE, null, ex);
	        }
        }
        try {
            userManager.save();
        } catch (IOException ex) {
            Logger.getLogger(QuitterTCP.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(QuitterTCP.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

